<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 账号登陆接口
 */
class Account extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    /**
     * 登陆
     *
     * @ApiMethod (POST)
     * @param string $account  账号(admin)
     * @param string $password 密码(123456)
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__('Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo()];
            $this->success(__('Logged in successful'), $data['userinfo']['token']);
        } else {
            $this->error($this->auth->getError());
        }
    }
}